
/** 
* Fetch the currency's rate
*
* @param HtmlObject         srcSymbol   the category selected
* @param HtmlCollections    trDate      trDate
* @param HtmlCollections    dstObject   the group selected
* 
*/
function getRate(srcSymbol, 
                 trDate, 
                 dstObject){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  trDate    = document.getElementsByName(trDate);
  trDate    = trDate[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  if(srcSymbol.length > 0){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_43/controllers/getRate.php" +"?sid="+Math.random();
		var params 	= "srcSymbol="    + srcSymbol +
		              "&trDate="      + trDate.value ;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			      dstObject.value = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
	}
}

/**
* unset $_SESSIOn["passedJobId"]
*
* @param  string  target url
*
*/
function cleanMemory(thisUrl){
  
  var result = confirm("The memory is not empty \nWould you like to clean it up? ");
  
  if(result == true){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= thisUrl +"?sid="+Math.random();
    
    var params 	= "cleanUp=1";
  	              
  	xmlHttp.open("POST", url, true);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
  		
  		/* Do Nothing */
  		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status + " response: " + xmlHttp.responseText);
  	}
  	
  	xmlHttp.send(params);
  }
}


/**
* set serial number based on srcObject number and dstObject
*
* @param  string  srcStart
* @param  String  srcLength
* @param  string  srcObject
* @param  String  dstObject
*
*/
function setSerNumber4(srcStart, 
                       srcLength,
                       srcObject, 
                       dstObject){

  var _STOP       = false; 
  var _RET_NULL   = " ";
  var _NO_NUMBER  = "_NO_NUMBER";
  
  var stsContinue = true;

  srcStart  = document.getElementsByName(srcStart);
  srcLength = document.getElementsByName(srcLength);
  srcObject = document.getElementsByName(srcObject);
  dstObject = document.getElementsByName(dstObject);
  
  if(srcStart.length > 0){
    
    srcStart = srcStart[0];
  }
  
  if(srcLength.length > 0){
    
    srcLength = srcLength[0];
  }
  
  if(srcObject.length > 0){
    
    srcObject = srcObject[0];
  }
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  /**
  * Make sure everything is not empty
  * 
  */
  if(dstObject.value != ""){
    
    if(srcStart.value == ""){
      
      stsContinue = _STOP;
    }
    
    if(srcLength.value == ""){
      
      stsContinue = _STOP;
    }
  }
  
  if(stsContinue == true){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_43/controllers/getSerNumber.php" + "?sid="+Math.random();
    
    var params 	= "srcStart="      + srcStart.value   +
                  "&srcLength="    + srcLength.value  +
                  "&srcTotal="     + srcObject.value  +
                  "&srcSerNumber=" + dstObject.value  ;
  	              
  	xmlHttp.open("POST", url, true);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
  		
  		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  		  alert(xmlHttp.responseText);
  		  switch(xmlHttp.responseText){
  		    
  		    case _RET_NULL:
  		      
  		      alert("Error: System has given up, calculation failed");
  		      break;
  	      
  	      case _NO_NUMBER:
  	      
  	        /* Do nothing */
  	        break;
  	      	      
          default:
          
            dstObject.value = xmlHttp.responseText;
  		  }
  		}
  	}
  	
  	xmlHttp.send(params);
  }
}

/**
* Process the miniInput
* save all entry into DB 
* and fetch the jobId of this entry
*
* @param  string  srcObject
* @param  string  srcVarName
* @param  string  dstVarName
* @param  string  srcJid
* @param  string  srcQuantity
* @param  string  srcDtlNo
* @param  string  srcSubNo
*
*/
function processMiniInput(srcObject,
                          srcVarName,
                          dstVarName,
                          srcJid,
                          srcQuantity,
                          srcDtlNo,
                          srcSubNo){
  
  var _CONN_ERROR         = "****";
  var _RET_NULL           = " ";
  var _NO_NUMBER          = "_NO_NUMBER";
  var _MANUAL_KEY_IN      = "_MANUAL_KEY_IN";
  var _DEFAULT_SEPARATOR  = "~^~";
  var _NOT_BALANCE        = "~**~";
  
  var srcInput = "";
  
  srcObject   = document.getElementsByName(srcObject);
  srcJid      = opener.document.getElementsByName(srcJid);
  dstVarName  = opener.document.getElementsByName(dstVarName);
  srcQuantity = opener.document.getElementsByName(srcQuantity);
  
  if(srcObject.length > 0){
    
    var maxLoop = srcObject.length;
    
    for(idx = 0; idx < maxLoop; idx++){
      
      srcInput = srcInput + srcObject[idx].value;
    }
  }else{
    
    srcInput = srcObject.value;
  }
  
  if(srcJid.length > 0){
    
    srcJid= srcJid[0];
  }
  
  if(dstVarName.length > 0){
    
    dstVarName = dstVarName[0];
  }
  
  if(srcQuantity.length > 0){
    
    srcQuantity = srcQuantity[0];
  }
  
  if(srcObject.value != ""){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_43/controllers/processMiniInput.php" + "?sid="+Math.random();
    
    var params 	= "srcInput="     + srcInput          +
                  "&srcJid="      + srcJid.value      +
                  "&srcQuantity=" + srcQuantity.value +
                  "&DTL_NO="      + srcDtlNo          +
                  "&SUB_NO="      + srcSubNo;
  	              
  	xmlHttp.open("POST", url, true);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
  		
  		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  		  
  		  //alert(xmlHttp.responseText);
  		  
  		  switch(xmlHttp.responseText){
  		    
  		    case _CONN_ERROR:
  		      
  		      alert("Error: Oracle Connection failed");
  		      break;
  		    
  		    case _RET_NULL:
  		      
  		      alert("Error: System has given up, calculation failed");
  		      break;
  	      
  	      case _NO_NUMBER:
  	      
  	        /* Do nothing */
  	        break;
  	      
  	      case _NOT_BALANCE:
          
            alert("Error: Entry is not equal to source Quantity");
            break;
  	      	      
          default:
                          
            dstVarName.value = _MANUAL_KEY_IN     + 
                               _DEFAULT_SEPARATOR + 
                               xmlHttp.responseText;
            
            /* window.close(); */
  		  }
  		}
  	}
  	
  	xmlHttp.send(params);
  }else{
    
    alert("Error: No Input");
  }
}

/**
* This is a test Function
* to test if the include is success
*
* @param  void
*
*/
function sayHelloAjax(){
  
  alert("ajax.js is linked");
}

var Ajax = {

  _RET_NULL   : " ",
  _CONN_ERROR : "****",
  
  firstElement : function (elm){
  
    if(elm.length > 0){
    
      return elm[0];
    }else{
    
      return elm;
    }
  },
  
  cvtQty : function(model, to, toQty, from, thisObj){
  
    model   = document.getElementsByName(model);
    to      = document.getElementsByName(to);
    toQty   = document.getElementsByName(toQty);
    from    = document.getElementsByName(from);
    
    model   = this.firstElement(model);
    to      = this.firstElement(to);
    toQty   = this.firstElement(toQty);
    from    = this.firstElement(from);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/processConvertion.php" +"?sid="+Math.random();
    var params  = "model="  + model.value +
                  "&to="    + to.value    +
                  "&toQty=" + toQty.value +
                  "&from="  + from.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.value = xmlHttp.responseText;
    }
  },
  
  unit  : function(model, from, thisObj){
  
    model = document.getElementsByName(model);
    
    if(model.length > 0){
    
      model = model[0];
    }
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/getUnit.php" +"?sid="+Math.random();
    var params  = "model=" + model.value +
                  "&from=" + from;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },
  
  model : function(isAll, thisObj){
  
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_12/controllers/getModel.php" +"?sid="+Math.random();
    var params  = "isAll="  + isAll +
                  "&model=" + thisObj.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },

  suppCstCode : function (suppCstCode,
                          suppCstEnt,
                          typeEnt){
    
    suppCstCode   = document.getElementsByName(suppCstCode);
    suppCstEnt    = document.getElementsByName(suppCstEnt);
    typeEnt       = document.getElementsByName(typeEnt);
    
    suppCstCode   = GMisc.firstElement(suppCstCode);
    suppCstEnt    = GMisc.firstElement(suppCstEnt);
    typeEnt       = GMisc.firstElement(typeEnt);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_31/controllers/getSuppCustCode.php" +"?sid="+Math.random();
    var params  = "type="         + typeEnt.value +
                  "&suppCstName=" + suppCstEnt.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    suppCstCode.value = xmlHttp.responseText;
  },

  suppCst : function (suppCstEnt,
                      typeEnt){
    
    suppCstEnt  = document.getElementsByName(suppCstEnt);
    typeEnt     = document.getElementsByName(typeEnt);
    
    suppCstEnt  = GMisc.firstElement(suppCstEnt);
    typeEnt     = GMisc.firstElement(typeEnt);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_31/controllers/getSuppCust.php" +"?sid="+Math.random();
    var params  = "type="    + typeEnt.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    suppCstEnt.innerHTML = xmlHttp.responseText;
  },

  crTerm : function (crTerm,
                     trDate,
                     dueDate){
                     
    crTerm  = document.getElementsByName(crTerm);
    trDate  = document.getElementsByName(trDate);
    dueDate = document.getElementsByName(dueDate);
    
    crTerm  = GMisc.firstElement(crTerm);
    trDate  = GMisc.firstElement(trDate);
    dueDate = GMisc.firstElement(dueDate);
    
    var url = "/ecs/tocmenu_43/controllers/getDateDifference.php";
    
    var params = "trDate="    + trDate.value  +
                 "&dueDate="  + dueDate.value ;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
            
      case this._CONN_ERROR:
        
        alert("Db connection failed");
        break;
        
      case this._RET_NULL:
        
        alert("Error has been occured");
        break;
        
      default:
        
        crTerm.value = responseText;
    }
  },

  setButton : function (tx,
                        customButton){
  
    tx            = document.getElementsByName(tx);
    customButton  = document.getElementsByName(customButton);
    
    tx            = GMisc.firstElement(tx);
    customButton  = GMisc.firstElement(customButton);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/getButton.php" +"?sid="+Math.random();
    var params  = "theTx="    + tx.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    customButton.innerHTML = xmlHttp.responseText;
  },

  sayHello : function(){
  
    alert("Ajax Linked");
  }
} 